package com.example.good.controller;

import com.example.good.entity.PageVO;
import com.example.good.entity.StudentDO;
import com.example.good.entity.StudentsVO;
import com.example.good.entity.request.AddStudentToClassRequest;
import com.example.good.entity.request.StudentQueryRequest;
import com.example.good.service.StudentService;
import com.example.good.utils.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


/**
 * 学生控制
 * 
 */
@RestController
@RequestMapping("/user/student")
public class StudentController {
    
    @Autowired
    StudentService studentService;

    /**
     * 根据学生名字或id获取学生
     *
     * @param nameOrId 名字或id
     * @return 返回一个学生详情列表
     */
    @GetMapping("/queryStudentByNameOrId")
    public Result<List<StudentDO>> queryAllStudent(@RequestBody String nameOrId){
        
        return Result.ok(studentService.getAllStudentByNameOrStudentId(nameOrId));
    }

    /**
     * 根据学生的姓名或班级名字获取学生
     * 
     * @param request 自定义请求对象
     * @return
     */
    @GetMapping("/queryAllStudents")
    public Result<PageVO<StudentsVO>> queryAllStudents(@RequestBody StudentQueryRequest request){
        return Result.ok(studentService.getAllStudentPage(request));
    }

    /**
     * 查看学生详情
     * 
     * @param studentId 学生id
     * @return
     */
    @GetMapping("/queryOneStudent")
    public Result<StudentsVO> queryOneStudent(String studentId){
        StudentsVO studentsDetail = studentService.getStudentsDetail(studentId);
        return Result.ok(studentsDetail);
    }
    
    
    @PostMapping("/saveStudent")
    public Result saveStudent(@RequestBody StudentsVO studentsVO){
        Boolean flag = studentService.updateStudent(studentsVO);

        return flag == true ? Result.ok("修改成功") : Result.error("修改失败");
    }

    /**
     * 班级添加学生
     * 
     * @param request 请求实体
     * @return
     */
    @PostMapping("/addStudentToClass")
    public Result addStudentToClass(@RequestBody AddStudentToClassRequest request){
        Boolean flag = studentService.addStudentToClass(request);
        return flag ? Result.ok("添加成功") : Result.error("添加失败"); 
    }

    /**
     * 班级删除学生
     * @param studentIds 学生id列表
     * @return
     */
    @PostMapping("/deleteStudentFromClass")
    public Result deleteStudentFromClass(@RequestBody List<String> studentIds){
        Boolean flag = studentService.deleteStudentFromClass(studentIds);
        return flag ? Result.ok("删除成功") : Result.error("删除失败");
    }
    
    /**
     * 查看没有加入班级的学生
     * 
     * @return 返回实体列表
     */
    @GetMapping("/queryNoClassStudent")
    public Result queryNoClassStudent(){
        return Result.ok(studentService.queryNoClassStudents());
    }
    
    
    public Result importStudentData(@RequestParam("file")MultipartFile multipartFile, HttpServletResponse response) throws IOException{
        return null;
    }
    
    
    
    
    
    
}
